package com.hsnn.medstgmini.trading.pur.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.trading.pur.dao.OrderConfirmDao;
import com.hsnn.medstgmini.trading.pur.model.OrderDetailStatus;
import com.hsnn.medstgmini.trading.pur.model.OrderIsRecepted;
import com.hsnn.medstgmini.trading.pur.model.form.OrderPackage;
import com.hsnn.medstgmini.trading.pur.service.OrderConfirmManager;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.ParseDate;
import com.hsnn.medstgmini.util.ParseNumber;

/**
 * @category 配送企业订单确认
 * @author 蔡春龙
 * @date 2015年7月13日
 */
@Service
public class OrderConfirmManagerImpl implements OrderConfirmManager {

	@Autowired
	private OrderConfirmDao orderConfirmDao;

	/**
	 * @category 分页查询订单包信息
	 * @date 2015年7月13日
	 * @param orderPackage
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Pagination getOrderConfirm(OrderPackage orderPackage, Pagination page) {
		page = orderConfirmDao.getOrderConfirm(orderPackage, page);
		List<Map<String,Object>> list = (List<Map<String, Object>>) page.getRows();
		Map<String,Object> map = new HashMap<String, Object>();
		if(list.size() > 0){
			for(int i = 0;i<list.size();i++){
				map = list.get(i);
				map.put("submitDatetime",
						map.get("submitDatetime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("submitDatetime").toString())));
			}
		}
		return page;
	}
	
	/**
	 * @category 分页查询订单包信息
	 * @date 2015年7月13日
	 * @param op
	 * @param page
	 * @return
	 */
	public Map<String, Object> getOrderPackageById(String id) {
		Map<String, Object> map =  orderConfirmDao.getOrderPackageById(id);
		map.put("submitDatetime",
				map.get("submitDatetime") == null ? "" : ParseDate
						.parseFullFormat(ParseDate.fromFullFormat(map
								.get("submitDatetime").toString())));
		
		map.put("orderConfirmTime",
				map.get("orderConfirmTime") == null ? "" : ParseDate
						.parseFullFormat(ParseDate.fromFullFormat(map
								.get("orderConfirmTime").toString())));
		return map;
	}

	/**
	 * @category 配送企业分页按包查询订单信息
	 * @date 2015年7月13日
	 * @param orderPackage
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Pagination getOrderConfirmDetail(OrderPackage orderPackage,
			Pagination page) {
		page = orderConfirmDao.getOrderConfirmDetail(orderPackage, page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page.getRows();
		if (null != list && list.size() > 0){
			for (int i = 0; i < list.size(); i++){
				Map<String, Object> map = list.get(i);
				map.put("orderStatus", map.get("orderStatus")==null?"":OrderDetailStatus.getName(ParseNumber.toInteger(map.get("orderStatus").toString())));
			}
		}
		return page;
	}
	
	/**
	 * @category 分页按包查询订单信息
	 * @date 2015年7月13日
	 * @param orderPackage
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Pagination getOrderConfirmDetailDel(OrderPackage orderPackage,
			Pagination page) {
		page = orderConfirmDao.getOrderConfirmDetailDel(orderPackage, page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page.getRows();
		if (null != list && list.size() > 0){
			for (int i = 0; i < list.size(); i++){
				Map<String, Object> map = list.get(i);
				map.put("orderStatus", map.get("orderStatus")==null?"":OrderDetailStatus.getName(ParseNumber.toInteger(map.get("orderStatus").toString())));
			}
		}
		return page;
	}

	
	
	/**
	 * @category 确认或拒绝
	 * @date 2015年11月25日
	 * @param orderPackIds
	 * @param user
	 * @param type1确认0拒绝
	 */
	@Transactional
	public void dispatch(String orderPackIds, SysUser user, int type) {
		orderConfirmDao.dispatch(orderPackIds, user, type);
	}
	
	/**
	 * @category 根据订单明细号查询配送信息列表
	 * @date 2015年7月13日
	 * @param orderPackage
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Pagination getOrderDelList(OrderPackage orderPackage, Pagination page) {
		page = orderConfirmDao.getOrderDelList(orderPackage, page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page.getRows();
		if (null != list && list.size() > 0){
			for (int i = 0; i < list.size(); i++){
				Map<String, Object> map = list.get(i);
				map.put("isRecepted", map.get("isRecepted")==null?"":OrderIsRecepted.getName(ParseNumber.toInteger(map.get("isRecepted").toString())));
			}
		}
		return page;
		
	}

}
